package com.conduit.comnands;

import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.conduit.commands.prototype.AbstractCommand;
import com.conduit.enums.ResultCode;

public class ValidateFileExistsCommand extends AbstractCommand {

	public ValidateFileExistsCommand() {
		// validate file exists <file name/ dir> <exists true/false>
		commandSyntax = "validate file exists\\s{2,}(.*)\\s{2,}(.*)";
	}

	@Override
	public void executeWithArgs(String args) {
		// validate command's syntax
		if (!args.matches(commandSyntax)) {
			setResult("Error in Command's syntax (\"" + args + "\")");
			setResultCode(ResultCode.Error);
			return;
		}
		Matcher matcher = Pattern.compile(commandSyntax).matcher(args);
		if (matcher.find()) {
			File fileName = new File(matcher.group(1));
			boolean isExpectedToBeExists = Boolean.parseBoolean(matcher.group(2).toLowerCase().trim());
			if (fileName.exists()){ // if file/dir exitsts
				if (isExpectedToBeExists){ //if file/dir was expected to be exists
					setResult("File/Dir "+fileName+" Was Found as Expected");
					setResultCode(ResultCode.Successes);
				}else{// if file/dir was not expected to be exists
					setResult("File/Dir "+fileName+" Was Found Not as Expected");
					setResultCode(ResultCode.Error);
				}
			}else { //if file/dir was not exists
				if (isExpectedToBeExists){ //if file/dir was expected to be exists
					setResult("File/Dir "+fileName+" Was Not Found Not as Expected");
					setResultCode(ResultCode.Error);
				}else{// if file/dir was not expected to be exists
					setResult("File/Dir "+fileName+" Was Not Found as Expected");
					setResultCode(ResultCode.Successes);
				}
			}
		}
	}

	@Override
	public String getCommandName() {

		return "Validate File/Dir Exists";
	}

}
